import re


from datetime import datetime


iso_format = re.compile(r"(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d).(\d\d\d\d\d\d)Z$")


class ParseError(Exception):
  pass


def to_isodate(a_datetime):
  return a_datetime.isoformat() + 'Z'


def from_isodate(date_string):
  m = iso_format.search(date_string)
  if not m:
    raise ParseError()
  (year, month, day, hour, minute, second, fraction) = m.groups()
  return datetime(int(year), int(month), int(day),
                  int(hour), int(minute), int(second), int(fraction), None)

